home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / c / mkid.lha / src / expand_args.c < prev    next >
C/C++ Source or Header  |  1995-06-29  |  3KB  |  168 lines

  1. /*
  2. **    expand_args.c
  3. **
  4. **        Expands wild card arguments found in the command line.
  5. **        Written by Olaf Barthel (olsen@sourcery.han.de)
  6. **        Public Domain
  7. **
  8. **    :ts=4
  9. */
  10.  
  11. #include <dos/dosextens.h>
  12. #include <dos/dosasl.h>
  13.  
  14. #include <exec/memory.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/dos_protos.h>
  18.  
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #define MAX_FILENAME_LEN 512
  23.  
  24. typedef struct NameNode
  25. {
  26.     struct NameNode    *Next;
  27.     char            *Name;
  28. } *pNameNode, NameNode;
  29.  
  30. extern struct DosLibrary *DOSBase;
  31.  
  32. int
  33. expand_args(int argc,char **argv,int *_argc,char ***_argv)
  34. {
  35.     NameNode            *Root        = NULL;
  36.     LONG                 NamePlus    = 0;
  37.     LONG                 NameTotal    = 0;
  38.     LONG                 Error        = 0;
  39.     struct AnchorPath    *Anchor;
  40.  
  41.     *_argc = argc;
  42.     *_argv = argv;
  43.  
  44.     if(DOSBase -> dl_lib . lib_Version < 37)
  45.         return(0);
  46.  
  47.     if(Anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath) + MAX_FILENAME_LEN,MEMF_ANY | MEMF_CLEAR))
  48.     {
  49.         int i;
  50.  
  51.         Anchor -> ap_Strlen        = MAX_FILENAME_LEN;
  52.         Anchor -> ap_BreakBits    = SIGBREAKF_CTRL_C;
  53.  
  54.         for(i = 0 ; !Error && i < argc ; i++)
  55.         {
  56.             if(i && ParsePatternNoCase(argv[i],Anchor -> ap_Buf,MAX_FILENAME_LEN) == 1)
  57.             {
  58.                 NameNode    *Node;
  59.                 LONG         Result;
  60.  
  61.                 Result = MatchFirst(argv[i],Anchor);
  62.  
  63.                 while(!Result)
  64.                 {
  65.                     if(Anchor -> ap_Info . fib_DirEntryType < 0)
  66.                     {
  67.                         if(Node = (NameNode *)malloc(sizeof(NameNode) + strlen(Anchor -> ap_Buf) + 1))
  68.                         {
  69.                             strcpy(Node -> Name = (char *)(Node + 1),Anchor -> ap_Buf);
  70.  
  71.                             Node -> Next = Root;
  72.  
  73.                             Root = Node;
  74.  
  75.                             NamePlus++;
  76.                             NameTotal++;
  77.                         }
  78.                         else
  79.                         {
  80.                             Result = ERROR_NO_FREE_STORE;
  81.                             break;
  82.                         }
  83.                     }
  84.  
  85.                     Result = MatchNext(Anchor);
  86.                 }
  87.  
  88.                 if(Result != ERROR_NO_MORE_ENTRIES)
  89.                     Error = Result;
  90.             }
  91.             else
  92.             {
  93.                 NameNode *Node;
  94.  
  95.                 if(Node = (NameNode *)malloc(sizeof(NameNode)))
  96.                 {
  97.                     Node -> Name = argv[i];
  98.  
  99.                     Node -> Next = Root;
  100.  
  101.                     Root = Node;
  102.  
  103.                     NameTotal++;
  104.                 }
  105.                 else
  106.                     Error = ERROR_NO_FREE_STORE;
  107.             }
  108.         }
  109.  
  110.         if(!Error && NamePlus)
  111.         {
  112.             char **Index;
  113.  
  114.             if(Index = (char **)malloc(sizeof(char *) * (NameTotal + 1)))
  115.             {
  116.                 NameNode *Node;
  117.  
  118.                 *_argc = NameTotal;
  119.                 *_argv = Index;
  120.  
  121.                 Index = &(Index[NameTotal]);
  122.  
  123.                 *Index-- = NULL;
  124.  
  125.                 Node = Root;
  126.  
  127.                 while(Node)
  128.                 {
  129.                     *Index-- = Node -> Name;
  130.  
  131.                     Node = Node -> Next;
  132.                 }
  133.             }
  134.             else
  135.                 Error = ERROR_NO_FREE_STORE;
  136.         }
  137.  
  138.         if(Error || !NamePlus)
  139.         {
  140.             NameNode *Node,*Next;
  141.  
  142.             Node = Root;
  143.  
  144.             while(Node)
  145.             {
  146.                 Next = Node -> Next;
  147.  
  148.                 free(Node);
  149.  
  150.                 Node = Next;
  151.             }
  152.         }
  153.  
  154.         FreeVec(Anchor);
  155.     }
  156.     else
  157.         Error = ERROR_NO_FREE_STORE;
  158.  
  159.     if(Error)
  160.     {
  161.         PrintFault(Error,argv[0]);
  162.  
  163.         return(-1);
  164.     }
  165.     else
  166.         return(0);
  167. }
  168.